package com.zhipu.balance.ui.vm.home.calculate.wheat

import android.text.TextUtils
import androidx.databinding.ObservableField
import com.zhipu.balance.mobile.event.Personnel
import com.zhipu.balance.mobile.event.RecordWheat
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.utils.CommonUtils

/**
 * 小麦面筋
 */
class WheatGlutenViewModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService){


    /**
     * 重量
     */
    var weighing : String? = null

    /**
     * 小麦实体
     */
    var mRecordWheat : RecordWheat? = null


    /**
     * 当前的检查人员
     */
    var mPersonnel : Personnel? = null


    /**
     * 面筋-试样质量
     */
    var glutenSampleQuality = ObservableField("")
    /**
     * 湿面筋质量
     */
    var wetGlutenQuality = ObservableField("")

    /**
     * 湿面筋含量 %
     */
    var wetGlutenResult = ObservableField("")
    /**
     * 干面筋质量
     */
    var dryGlutenQuality = ObservableField("")
    /**
     * 干面筋含量 %
     */
    var dryGlutenResult = ObservableField("")
    /**
     * 面筋吸水率结果
     */
    var glutenWaterAbsorptionResults = ObservableField("")





    /**
     * 更新页面
     */
    fun updateRecordRice(){
        if(!TextUtils.isEmpty(mRecordWheat?.glutenRecorder)) {
            //面筋-试样质量
            glutenSampleQuality.set(mRecordWheat?.glutenSampleQuality.toString())
            //湿面筋质量
            wetGlutenQuality.set(mRecordWheat?.wetGlutenQuality.toString())
            //湿面筋含量 %
            wetGlutenResult.set(mRecordWheat?.wetGlutenResult.toString())
            //干面筋质量
            dryGlutenQuality.set(mRecordWheat?.dryGlutenQuality.toString())
            //干面筋含量 %
            dryGlutenResult.set(mRecordWheat?.dryGlutenResult.toString())
            //面筋吸水率结果
            glutenWaterAbsorptionResults.set(mRecordWheat?.glutenWaterAbsorptionResults.toString())
        }
    }


    /**
     * 释放更新页面
     */
    fun releaseRecordRice(){
        //面筋-试样质量
        glutenSampleQuality.set("")
        //湿面筋质量
        wetGlutenQuality.set("")
        //湿面筋含量 %
        wetGlutenResult.set("")
        //干面筋质量
        dryGlutenQuality.set("")
        //干面筋含量 %
        dryGlutenResult.set("")
        //面筋吸水率结果
        glutenWaterAbsorptionResults.set("")
    }






    /**
     * 面筋-试样质量
     */
    fun BulkQuality(){
        glutenSampleQuality.set(weighing)
        mRecordWheat?.glutenSampleQuality = glutenSampleQuality.get()?.toDouble()
    }

    /**
     * 湿面筋质量
     */
    fun WetGlutenQuality(){
        wetGlutenQuality.set(weighing)
        mRecordWheat?.wetGlutenQuality = wetGlutenQuality.get()?.toDouble()
    }

    /**
     * 干面筋质量
     */
    fun DryGlutenQuality(){
        dryGlutenQuality.set(weighing)
        mRecordWheat?.dryGlutenQuality = dryGlutenQuality.get()?.toDouble()
    }


    /**
     * 计算
     */
    fun calculation(){

        //湿面筋含量
        val mWetGlutenResult = CommonUtils.formatDouble(mRecordWheat!!.wetGlutenQuality / mRecordWheat!!.glutenSampleQuality * 100)
        wetGlutenResult.set(mWetGlutenResult.toString())
        mRecordWheat!!.wetGlutenResult = mWetGlutenResult
        //干面筋含量
        val mDryGlutenResult: Double = CommonUtils.formatDouble(mRecordWheat!!.dryGlutenQuality / mRecordWheat!!.glutenSampleQuality * 100)
        dryGlutenResult.set(mDryGlutenResult.toString())
        mRecordWheat!!.dryGlutenResult = mDryGlutenResult
        //面筋吸水率结果
        val mGlutenWaterAbsorptionResults: Double = CommonUtils.formatDouble((((mWetGlutenResult - mDryGlutenResult) / mDryGlutenResult) * 100))
        mRecordWheat!!.glutenWaterAbsorptionResults = mGlutenWaterAbsorptionResults
        glutenWaterAbsorptionResults.set(mGlutenWaterAbsorptionResults.toString())
        /**
         * 面筋记录员
         */
        mRecordWheat?.glutenRecorder = mPersonnel?.name


        //试样质量
        if(mRecordWheat?.glutenSampleQuality == 0.00){
            glutenSampleQuality.set("0.00")
        }
        //湿面筋质量
        if(mRecordWheat?.wetGlutenQuality == 0.00){
            wetGlutenQuality.set("0.00")
        }
        //干面筋质量
        if(mRecordWheat?.dryGlutenQuality == 0.00){
            dryGlutenQuality.set("0.00")
        }

    }


}